package c2.group.business.gradepvp;

import c2.advanced.business.base.KvData;
import c2.group.business.GroupBody;
import c2.group.business.base.GroupModule;
import c2.group.business.base.GroupSubModule;
import cate.game.play.part.PlaySide;
import cate.game.pvp.gradepvp.GradePvpHelper;
import cate.game.pvp.gradepvp.GradePvpPeriod;
import cate.game.pvp.gradepvp.dto.GradePvpPlayerData;
import cate.game.role.RoleSimpleBase;
import cate.game.role.pvp.gradepvp.GradePvpRecord;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class GradePvpModule extends GroupModule<GradePvpKv> {
    public GradePvpPoolModule pool = new GradePvpPoolModule();
    public GradePvpRecvModule recv = new GradePvpRecvModule();

    public GradePvpModule(GroupBody context) {
        super(context);
        GradePvpPeriod.initialize();
    }

    @Override
    protected String getStatusKey() {
        return KvData.KEY_GRADE_PVP;
    }

    @Override
    public String getName() {
        return "全服段位赛";
    }

    public GradePvpPlayerData match(String playerUid, int star) {
        return pool.match(playerUid, star);
    }

    @Override
    protected GroupSubModule<?>[] getSubModules() {
        return new GroupSubModule[]{pool, recv};
    }

    public void fightOver(PlaySide ps, int star, int score, GradePvpRecord record) {
        pool.updatePlayers(ps, star, score);
        status.addRecords(record);
    }

    public List<GradePvpRecord> loadNewBeeRecords() {
        return status.loadNewBeeRecords();
    }

    public List<RoleSimpleBase> loadSixGod(int season) {
        return status.loadSixGods(season);
    }

    public int loadMyRank(String roleUid) {
        return pool.loadMyRank(roleUid);
    }

    @Override
    public void onInterval() {
        if(status.startTime == 0){
            return;
        }
        int currentSeason = GradePvpHelper.calcCurrentSeason(status.startTime);
        if(currentSeason == 0){
            return;
        }
        if(currentSeason < status.season){
            status.season = currentSeason;
            GradePvpPeriod currentPeriod = GradePvpHelper.currentSeasonPeriod(status.startTime);
            status.period = currentPeriod.ordinal();
            return;
        }
        //赛季判断
        if(status.season != currentSeason){
            status.season = currentSeason;
            log.info("进入新赛季：S{}", currentSeason);
        }
        //允许宕机很久导致的状态多次变化
        GradePvpPeriod currentPeriod = GradePvpHelper.currentSeasonPeriod(status.startTime);
        if(status.period != currentPeriod.ordinal()){
            int times = 0;
            log.info("马上进入赛季{}阶段", currentPeriod);
            GradePvpPeriod st = GradePvpPeriod.values()[status.period];
            while(currentPeriod.ordinal() != st.ordinal()){
                log.info("当前为赛季{}阶段",st);
                st = GradePvpPeriod.getNextStatus(st);
                status.period = st.ordinal();
                executeStatus(st);
                times ++;
                //如果20次还没有结束循环，说明状态有问题，强制改成当前状态,防止死循环
                if(times > 20){
                    status.period = currentPeriod.ordinal();
                    break;
                }
            }
            log.info("进入赛季{}阶段", currentPeriod);
        }
    }

    private void executeStatus(GradePvpPeriod period) {
        if (period.equals(GradePvpPeriod.SECOND_HALF_SETTLE)) {
            log.info("第{}赛季结束，开始结算六神", status.season);
            status.updateSixGod(pool.sixGod());
            pool.reset();
        }
    }

    @Override
    protected int getIntervalSeconds() {
        return 2;
    }
}
